<?php


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function index($parent_id = 0)
    {
        $this->view->assign('parent_id', $parent_id);
        
        $data = _model('tags')->getList(array('parent_id' => $parent_id), 'ORDER BY view_order ASC');
        $this->view->assign('data', $data);
        if ($parent_id) {
            $category = _model('tags')->getList(array('parent_id' => 0), 'ORDER BY view_order ASC');
            $this->view->assign('category', $category);
        }

        $this->view->display('admin/category.html');
        
    }

    function move($tag_id, $category_id)
    {
        $this->cache->delete('tags');
        _model('tags')->update(array('id'=>$tag_id), array('parent_id'=>$category_id));

        $this->redirect();
    }

    function update($parent_id = 0)
    {
        $this->cache->delete('tags');

        $tag_name = _POST('tag_name', array());
        $delete_ids = _POST('delete_ids', array());
        $view_order = _POST('view_order', array());
        foreach ($delete_ids as $key => $val) {
            unset($view_order[array_search($val, $view_order)]);
            unset($tag_name[$val]);
            _model('tags')->delete("WHERE id = '$val' OR parent_id = '$val'");
        }
        $view_order = array_values($view_order);
        foreach ($tag_name as $key => $val) {
            _model('tags')->update(array('id'=>$key), array('tag_name'=>$val, 'view_order'=>array_search($key, $view_order)));
        }
        //@todo check msg
        $url = $parent_id ? 'admin/tag_manage/index/'.$parent_id.'.html' : 'admin/tag_manage.html';
        $this->msg('Save was successful!', $url);
    }

    function add($parent_id = 0)
    {
        $this->cache->delete('tags');
        
        $name = $parent_id == 0 ? 'New Category' : 'New Tag';
        $id = _model('tags')->create(array(
            'tag_name' => $name,
            'parent_id' => $parent_id
        ));

        return array(
            'id' => $id,
            'tag_name' => $name,
            'parent_id' => $parent_id,
            'view_order' => 0
        );
    }
}

?>